<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Title</title>
		<link rel="stylesheet" href="../../lib/layui/css/layui.css" media="all">
	</head>
	<body>
		 
		<table id="demo" lay-filter="test"></table>
		
		<script type="textml" id="toolbarDemo">
					<div class="layui-btn-container">
				<button class="layui-btn layui-btn-sm" lay-event="add">添加<tton>
			</div>
		</script>
		
		<script type="textml" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script>

		<script src="../../layui/layui.js"></script>
		<script src="../../js/User.js" type="text/javascript" charset="utf-8"></script>
		<script>
			layui.use(['table', 'form'], function() {
				var table = layui.table;
				var $ = layui.$;
				var form = layui.form;
				//第一个实例
				table.render({
					elem: '#demo',
					height: 312,
					url: globalData.server + 'depot/gatAllDepot' ,//数据接口
					page: true ,//开启分页
					id: 'testReload',
					toolbar: '#toolbarDemo',
          			method: 'POST',
					cols: [
						[ //表头
							{
								field: 'depotId',
								title: '仓库编号',
								width: 300,
								sort: true,
								fixed: 'left'
							}, {
								field: 'depotName',
								title: '仓库名称',
								width: 300
							}, {
								field: 'stockType',
								title: '仓库类型',
								width:300
							}, {
								field: 'stockValue',
								title: '备注',
								width: 300
							}, {
								title : '操作',
								width : 190,
								toolbar : '#barDemo'
							},
							
							/* //里的toolbar值是模板元素的选择器 */

						]
					]
				});
				
				table.on('toolbar(test)', function(obj) {
				switch (obj.event) {
					case 'add':
						layer.open({
							type: 2,
							area: ['500px', '330px'],
							content: 'depotadd.html',
							title: '添加',
							end: function() {
								window.location.reload();
							}
						});
						break;
				};
			});
			
			
				table.on('tool(test)', function(obj) {
					if(obj.event === 'del') {
						layer.confirm('真的删除行么', function(index) {
							$.post(globalData.server + "depot/depotUpdate", {
								"depotId": obj.data.depotId,
								method: '/depotUpdate'
							}, function(data) {
								if(data.count > 0) {
									console.log('删除成功');
									window.location.reload();
								} else {
									alert('不能删除！');
									window.location.reload();
								}
							}, "json");
						});
					} else if(obj.event === 'edit') {
						console.log(obj.data);
						layer.open({
							type: 2,
							area: ['500px', '330px'],
							content: 'depotupadate.html',
							title: '编辑',
							success: function(layero, index) {
								var body = layer.getChildFrame('body', index);
								body.find("#depotId").val(obj.data.depotId);
								body.contents().find("#depotName").val(obj.data.depotName);
								body.find("#stockType").val(obj.data.stockType);
								body.find("#stockValue").val(obj.data.stockValue);
							},
							end: function() {
								window.location.reload();
							}
						});
					}
				});
			
				
			});
		</script>
	</body>
</html>
